package com.ossjk.oa.advisory.controller;

import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.baomidou.mybatisplus.mapper.Condition;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.ossjk.core.base.controller.BaseController;
import com.ossjk.core.util.CommonUtil;
import com.ossjk.oa.advisory.entity.Project;
import com.ossjk.oa.advisory.service.IDisciplineService;
import com.ossjk.oa.advisory.service.IProjectService;
import com.ossjk.oa.advisory.service.IProjectgroupService;
import com.ossjk.oa.advisory.service.ISchoolService;
import com.ossjk.oa.advisory.service.ISubprojectService;

/**
 * <p>
 * 项目管理 前端控制器
 * </p>
 *
 * @author Jason
 * @since 2019-01-14
 */
@RestController
@RequestMapping("/advisory/project")
public class ProjectController extends BaseController {
	@Autowired
	private IProjectService iProjectService;
	@Autowired
	private ISubprojectService iSubprojectService;
	@Autowired
	private IProjectgroupService iProjectgroupService;
	@Autowired
	private ISchoolService iSchoolService;
	@Autowired
	private IDisciplineService iDisciplineService;

	@RequiresPermissions("00150015")
	@GetMapping("/list")
	public Object list(String name, String sid, String sttm, String edtm, Page page) {
		Wrapper wrapper = Condition.create().isWhere(true).ne("p.isdelete", 2).orderBy("crtm");
		if (!CommonUtil.isBlank(name)) {
			wrapper.like("p.name", name);
		}
		if (!CommonUtil.isBlank(sid)) {
			wrapper.eq("p.sid", sid);
		}
		if (!CommonUtil.isBlank(sttm)) {
			wrapper.ge("p.sttm", sttm);
		}
		if (!CommonUtil.isBlank(edtm)) {
			wrapper.le("p.edtm", edtm);
		}
		page = iProjectService.selectVoPage(page, wrapper);
		return responseSuccess(page);
	}

	@RequiresPermissions("001000000005")
	@PostMapping("/insert")
	public Object insert(Project project, String[] spname, String[] did, String[] syears, String[] sremarks, String[] pgeid, String[] pgpid, String[] pgtype, String[] pgremarks) {
		if (iProjectService.insert(project, spname, did, syears, sremarks, pgeid, pgpid, pgtype, pgremarks)) {
			return responseSuccess();
		} else {
			return responseFail();
		}
	}

	@RequiresPermissions("001000000015")
	@PutMapping("/updateDetail")
	public Object updateDetail(Project project, String[] spname, String[] did, String[] syears, String[] sremarks, String[] pgeid, String[] pgpid, String[] pgtype, String[] pgremarks) {
		if (iProjectService.updateDetail(project, spname, did, syears, sremarks, pgeid, pgpid, pgtype, pgremarks)) {
			return responseSuccess();
		} else {
			return responseFail();
		}
	}

	@RequiresPermissions("001000000015")
	@GetMapping("/toUpdateDetail/{id}")
	public Object toUpdateDetail(@PathVariable("id") String id) {
		Map map = new HashMap();
		Project project = iProjectService.selectById(id);
		map.put("project", iProjectService.selectById(id));
		Wrapper wrapper = Condition.create().like("pid", id).orderBy("crtm");
		map.put("subproject", iSubprojectService.selectList(wrapper));
		Wrapper wrapper2 = Condition.create().isWhere(true).ne("pg.isdelete", "2").orderBy("crtm", true);
		if (!CommonUtil.isBlank(id)) {
			wrapper2.eq("pg.pid", id);
		}
		map.put("projectgroup", iProjectgroupService.selectGroup(wrapper2));
		return responseSuccess(map);
	}

	@RequiresPermissions("001500150020")
	@GetMapping("/toUpdate/{id}")
	public Object toUpdate(@PathVariable("id") String id) {
		Map map = new HashMap();
		Project Project = iProjectService.selectById(id);
		map.put("Project", Project);
		return responseSuccess(map);
	}

	@RequiresPermissions("001500150020")
	@PutMapping("/update")
	public Object update(Project Project, String[] areaIds) {
		if (iProjectService.updateById(Project)) {
			return responseSuccess();
		} else {
			return responseFail();
		}

	}

	@RequiresPermissions("001500150005")
	@DeleteMapping("/delete/{id}")
	public Object delete(@PathVariable("id") String[] id) {
		if (iProjectService.deleteBatchIds(Arrays.asList(id))) {
			return responseSuccess();
		} else {
			return responseFail();
		}
	}

}
